<template>
  <div class="wrapper" ref="wrapper">
    <div class="content"> 
      <slot></slot>
    </div>
  </div>
</template>

<script>
//引入better-scroll
import BScroll from 'better-scroll';

export default {
  name: 'Scroll',
  data() {
    return {
      scroll: null
    }
  },
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  mounted() {
    
    this.scroll = new BScroll(this.$refs.wrapper, {// BScroll实例
      probeType: this.probeType,
      click: true,
      pullUpLoad: this.pullUpLoad
    })  
    
    if(this.probeType !== 0 && this.probeType !== 1){ // 0,1都不会调用该方法
      this.scroll.on('scroll', position => {// 滚动传递给HOME
        this.$emit('scroll', position);
      })
    }

    if(this.pullUpLoad) {// 判断this.pullUpLoad
      this.scroll.on('pullingUp', () => {// 上拉加载传递给HOME
        this.$emit('pullingUp');
      })
    }


  },//mounted 结束
  methods: {
    scrollTop(x, y, time=300) {//返回顶部事件
      this.scroll && this.scroll.scrollTo(x, y, time);
    },       
    refresh() {// 一个图片加载完成执行一次BScroll刷新
      // console.log('刷新');
    
      this.scroll && this.scroll.refresh();
    },
    finishPullUp() {// 上拉加载更多
      this.scroll && this.scroll.finishPullUp();
    },
    getscrollY() {// 获取离开时的高度
      return this.scroll.y;
    },
    scrollBack(y) {// 返回到离开时的高度,并刷新一次
      this.scroll && this.scroll.scrollTo(0, y);
      this.refresh();
    }
  }
}
</script>

<style scoped>
  
</style>
